package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * 19楼空间
 */
public class _19louBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url){
		boolean bRet = false;
		String[] regex = {
				"http://[a-zA-Z0-9]*.19lou.com/forum-[0-9]+-thread-[0-9]+-1-1.html",
				"http://www.ihome99.com/forum-[0-9]*-thread-[0-9]*-1-1.html",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String author = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("h1").text();

		Element firstDiv = doc.select("div.first[itemprop=post]").first();
		if(firstDiv!=null){
			date = DateUtils.matchDate(firstDiv.select(".cont-top-left li[title]").attr("title"));
			content = HtmlCleaner.getContentHtml(url,  firstDiv.select(".thread-cont"));
			author = firstDiv.select(".author .uname").text();
		}
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select("#act-intro"));
		}
		try {
			if(date==null) date = DateUtils.matchDate(doc.select("span[title^=发表于]").first().attr("title"));
		} catch (Exception e) {
		}
		if(date==null) date = DateUtils.matchDate(doc.select("#act-property").text());
		if(StringUtils.isBlank(content)) {
			content = HtmlCleaner.getContentHtml(url,  doc.select(".post-cont").first());
		}
		if(StringUtils.isBlank(author)){
			try {
				author = doc.select(".post-hd .name").first().text();
			} catch (Exception e) {
			}
		}
		if(StringUtils.isBlank(author)){
			author = doc.select("#sponsor dt a").text();
		}
		if(StringUtils.isBlank(author)){
			author = doc.select("p.user-name").text();
		}

		clickNum = NumberUtils.matchNumber(doc.select("#J_threadViewNum").text());
		commentNum = NumberUtils.matchNumber(doc.select("#J_threadReplyNum").text());
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setAuthor(author);
		bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}
    
	
}
